package org.example.authservice.controller;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.authservice.anno.Debounce;
import org.example.authservice.pojo.Permission;
import org.example.authservice.pojo.Result;
import org.example.authservice.pojo.Role;
import org.example.authservice.pojo.SavePermissionParm;
import org.example.authservice.service.PermissionRoleService;
import org.example.authservice.service.PermissionService;
import org.example.authservice.service.RoleService;
import org.example.authservice.vo.SelectItem;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @author 浦慧慧
 * @since 2024/12/15 11.34
 * @version 1.0
 */
@RestController
@RequestMapping("/roles")
@Slf4j
public class RoleController {

    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    @Resource
    private PermissionRoleService permissionRoleService;

    @GetMapping("/search")
    public Result<?> search(){
        List<Role> list = roleService.getAllRoles();
        for (Role role : list) {
            //根据role的id查询出对应拥有的权限id
            //权限列表
            List<Permission> permissionList = new ArrayList<>();
            List<Integer> permissionRoles = roleService.getPermissionRoleById(role.getId());
            for (Integer permissionId : permissionRoles) {
                //根据权限id查询对应权限
                Permission permission = permissionService.selectById(permissionId);
                permissionList.add(permission);
            }
            role.setPermissionList(permissionList);
        }
        return Result.success(list);
    }
    /**
     * 新增角色
     * @param role 角色对象
     * @return Result    成功或失败
     */
    @PostMapping
    public Result<?> add(@RequestBody Role role){
        role.setIsDefault(1);
        roleService.save(role);
        return Result.success("新增成功");
    }

    /**
     * 编辑角色
     * @param role 角色对象
     * @return Result    成功或失败
     */
    @PutMapping
    public Result<?> edit(@RequestBody Role role) {
        // 处理接收到的 role 对象
        if(roleService.updateById(role)){
            return Result.success("编辑成功");
        }
        return Result.error("编辑失败");
    }
    /**
     * 删除角色
     * @param id 角色id
     * @return Result    成功或失败
     */
    @Debounce()
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable("id") Integer id) {
        // 判断是否为默认角色
        if (roleService.isDefaultRole(id)) {
            return Result.error("默认角色无法删除");
        }
        // 判断是否有用户拥有这个角色
        if (roleService.hasUsersWithRole(id)) {
            return Result.error("有用户拥有这个角色，不能删除");
        }
        if (roleService.removeById(id)) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 获取角色列表
     * @return Result    角色列表
     */
    @Debounce()
    @GetMapping
    public Result<?> selectRoleList(){
        List<Role> list= roleService.list();
        //返回的值
        List<SelectItem> selectItems = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .forEach(item ->{
                    SelectItem selectItem = new SelectItem();
                    selectItem.setCheck(false);
                    selectItem.setLabel(item.getComment());
                    selectItem.setValue(item.getId());
                    selectItems.add(selectItem);
                });
        return Result.success(selectItems);
    }

    /**
     * 保存角色权限
     * @param parm 权限参数
     * @return Result    成功或失败
     */
    @PostMapping("/{parm}/savePermissionRole")
    public Result<?> savePermissionRole(@PathVariable SavePermissionParm parm){
        permissionRoleService.savePermissionRole(parm);
        return Result.success("分配成功");
    }
}
